var vm = new Vue({
    el:'#app',
    data:{
        //表中所有点位的集合（包含点位属性）
        tableData: [],
        dataForm:{
            key:'',
        },
        //所有选中点位的集合（包含点位属性）
        dataListSelections: [],
        downloadDayCheckbox: false,
        downloadHourCheckbox: false,
        downloadMinuteCheckbox: false,
        uploadDayCheckbox: false,
        uploadHourCheckbox: false,
        uploadMinuteCheckbox: false,
        datatypeMaxCheckbox: false,
        datatypeMinCheckbox: false,
        datatypeAveCheckbox: false,
        datatypeDiffCheckbox: false,
        sourceTypeCoralCheckbox: false,
        sourceTypeXhaCheckbox: false,
        search:'',
        // loadingShow: false
    },
    created(){
        this.getTableData();
        this.bindAjax();
    },
    methods: {
        //选项有变动就自动勾选该行
        // TODO 变动取消取消勾选的功能暂时搁置
        listChange:function (scope) {
            //第二个参数：只把该行选中
            this.$refs.multipleTable.toggleRowSelection(scope.row,true);
        },
        //把所有首列选中（仅选中，不取消）
        setTrueAllSelections:function () {
            //用于每列顶部表示全选复选框改动时，全选首列
            this.tableData.forEach((data) => {
                this.$refs.multipleTable.toggleRowSelection(data,true);
            });
        },
        getRowKey:function (row) {
            return row.id;
        },
        bindAjax:function () {
            //在ajax请求提交后完成前隐藏表格，避免再次提交；完成后显示表格
            $(document).bind("ajaxSend", function(){
                // console.log(1)
                $("#elTable").hide();
                // this.loadingShow = true;
            }).bind("ajaxComplete", function(){
                // console.log(2)
                $("#elTable").show();
                // this.loadingShow = false;
            });
        },
        getTableData:function () {
            that = this;
            $.ajax({
                url:'/multipower/taskConfig/getAll',
                success:function (data) {
                    let res = [];
                    for(let i=0;i<data.data.length;i++){
                        res.push({
                            id: data.data[i].id,
                            pointId: data.data[i].pointId,
                            name: data.data[i].name,
                            devCode: data.data[i].devCode,
                            intervalTypeDownloadList: data.data[i].intervalTypeDownloadList,
                            intervalTypeUploadList: data.data[i].intervalTypeUploadList,
                            statisticType: data.data[i].statisticType,
                            sourceType: data.data[i].sourceType
                        });
                    }
                    that.tableData = [...res];
                },
                failure:function () {
                    that.$message({
                        type: 'info',
                        message: '获取初始表数据失败!'
                    });
                }
            })
        },
        batchEdit: function () {
            that = this;
            let res={ taskConfigDTOS: [] };
            for(let i=0;i<that.dataListSelections.length;i++){
                res.taskConfigDTOS.push({
                    id: that.dataListSelections[i].id,
                    pointId: that.dataListSelections[i].pointId,
                    name: that.dataListSelections[i].name,
                    devCode: that.dataListSelections[i].devCode,
                    intervalTypeDownloadList: that.dataListSelections[i].intervalTypeDownloadList,
                    intervalTypeUploadList: that.dataListSelections[i].intervalTypeUploadList,
                    statisticType: that.dataListSelections[i].statisticType,
                    sourceType: that.dataListSelections[i].sourceType
                });
            }
            // console.log(JSON.stringify(res));
            that.$confirm('确定批量修改吗？','提示',{
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(()=>{
                $.ajax({
                    type:'POST',
                    url:'/multipower/taskConfig/update',
                    dataType: "json",
                    contentType: "application/json",
                    data: JSON.stringify(res),
                    success:function () {
                        that.$message({
                            type: 'success',
                            message: '批量修改成功!'
                        });
                        //清空多选框选择状态
                        that.$refs.multipleTable.clearSelection();
                        //刷新表数据
                        that.getTableData();
                    },
                    failure:function () {
                        that.$message({
                            type: 'info',
                            message: '批量修改失败'
                        });
                    }
                })
            }).catch(()=>{
                that.$message({
                    type: 'info',
                    message: '已取消'
                });
            })
        },
        selectionChangeHandle: function (val) {
            this.dataListSelections = val;
        },
        //10个总复选框的控制逻辑
        checkboxChange:function (flag,checkbox,num) {
            //由不选到勾选
            if (flag == true) {
                this.$confirm('此操作将改变所有记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //确认
                    //按类别全选的逻辑
                    for (let i = 0; i < this.tableData.length; i++) {
                        if(checkbox==="download"&&!this.tableData[i].intervalTypeDownloadList.includes(num)){
                            this.tableData[i].intervalTypeDownloadList.push(num);
                        }else if(checkbox==="upload"&&!this.tableData[i].intervalTypeUploadList.includes(num)){
                            this.tableData[i].intervalTypeUploadList.push(num);
                        }else if(checkbox==="dataType"&&this.tableData[i].statisticType!=num){
                            this.tableData[i].statisticType = num;
                        }else if(checkbox==="sourceType"&&this.tableData[i].sourceType!=num){
                            this.tableData[i].sourceType = num;
                        }
                    }

                    this.setTrueAllSelections();

                    this.$message({
                        type: 'success',
                        message: '修改成功!'
                    });
                }).catch(() => {
                    //取消
                    //把复选框状态改回来
                    if(checkbox==="download"&&num==="3"){
                        this.downloadDayCheckbox = false;
                    }else if(checkbox==="download"&&num==="2"){
                        this.downloadHourCheckbox = false;
                    }else if(checkbox==="download"&&num==="1"){
                        this.downloadMinuteCheckbox = false;
                    }else if(checkbox==="upload"&&num==="3"){
                        this.uploadDayCheckbox = false;
                    }else if(checkbox==="upload"&&num==="2"){
                        this.uploadHourCheckbox = false;
                    }else if(checkbox==="upload"&&num==="1"){
                        this.uploadMinuteCheckbox = false;
                    }else if(checkbox==="dataType"&&num==="max"){
                        this.datatypeMaxCheckbox = false;
                    }else if(checkbox==="dataType"&&num==="min"){
                        this.datatypeMinCheckbox = false;
                    }else if(checkbox==="dataType"&&num==="ave"){
                        this.datatypeAveCheckbox = false;
                    }else if(checkbox==="dataType"&&num==="diff"){
                        this.datatypeDiffCheckbox = false;
                    }else if(checkbox==="sourceType"&&num==="1"){
                        this.sourceTypeCoralCheckbox = false;
                    }else if(checkbox==="dataType"&&num==="2"){
                        this.sourceTypeXhaCheckbox = false;
                    }
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            }
            //由勾选到取消
            else {
                this.$confirm('此操作将改变所有记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    for (let i = 0; i < this.tableData.length; i++) {
                        if(checkbox==="download"){
                            this.tableData[i].intervalTypeDownloadList =
                                this.tableData[i].intervalTypeDownloadList.filter(val => val!=num);
                        }else if(checkbox==="upload"){
                            this.tableData[i].intervalTypeUploadList =
                                this.tableData[i].intervalTypeUploadList.filter(val => val!=num);
                        }else if(checkbox==="dataType"){
                            this.tableData[i].statisticType = '';
                                // this.tableData[i].statisticType.filter(val => val!=num);
                        }else if(checkbox=="sourceType"){
                            this.tableData[i].sourceType = '';
                        }
                    }

                    this.setTrueAllSelections();

                    this.$message({
                        type: 'success',
                        message: '修改成功!'
                    });
                }).catch(() => {
                    if(checkbox==="download"&&num==="3"){
                        this.downloadDayCheckbox = true;
                    }else if(checkbox==="download"&&num==="2"){
                        this.downloadHourCheckbox = true;
                    }else if(checkbox==="download"&&num==="1"){
                        this.downloadMinuteCheckbox = true;
                    }else if(checkbox==="upload"&&num==="3"){
                        this.uploadDayCheckbox = true;
                    }else if(checkbox==="upload"&&num==="2"){
                        this.uploadHourCheckbox = true;
                    }else if(checkbox==="upload"&&num==="1"){
                        this.uploadMinuteCheckbox = true;
                    }else if(checkbox==="dataType"&&num==="max"){
                        this.datatypeMaxCheckbox = true;
                    }else if(checkbox==="dataType"&&num==="min"){
                        this.datatypeMinCheckbox = true;
                    }else if(checkbox==="dataType"&&num==="ave"){
                        this.datatypeAveCheckbox = true;
                    }else if(checkbox==="dataType"&&num==="diff"){
                        this.datatypeDiffCheckbox = true;
                    }else if(checkbox==="sourceType"&&num==="1"){
                        this.sourceTypeCoralCheckbox = true;
                    }else if(checkbox==="dataType"&&num==="2"){
                        this.sourceTypeXhaCheckbox = true;
                    }
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            }
        },
        downloadDayCheckboxChange: function (flag) {
            this.checkboxChange(flag,"download",'3');
        },
        downloadHourCheckboxChange: function (flag) {
            this.checkboxChange(flag,"download",'2');
        },
        downloadMinuteCheckboxChange: function (flag) {
            this.checkboxChange(flag,"download",'1');
        },
        uploadDayCheckboxChange: function (flag) {
            this.checkboxChange(flag,"upload",'3');
        },
        uploadHourCheckboxChange: function (flag) {
            this.checkboxChange(flag,"upload",'2');
        },
        uploadMinuteCheckboxChange: function (flag) {
            this.checkboxChange(flag,"upload",'1');
        },
        datatypeMaxCheckboxChange: function (flag) {
            this.checkboxChange(flag,"dataType",'max');
            //对于单选框，又未选中改为选中后，要取消其他框的选中状态
            if(flag){
                this.datatypeMinCheckbox = false;
                this.datatypeAveCheckbox = false;
                this.datatypeDiffCheckbox = false;
            }
        },
        datatypeMinCheckboxChange: function (flag) {
            this.checkboxChange(flag,"dataType",'min');
            if(flag){
                this.datatypeMaxCheckbox = false;
                this.datatypeAveCheckbox = false;
                this.datatypeDiffCheckbox = false;
            }
        },
        datatypeAveCheckboxChange: function (flag) {
            this.checkboxChange(flag,"dataType",'ave');
            if(flag){
                this.datatypeMaxCheckbox = false;
                this.datatypeMinCheckbox = false;
                this.datatypeDiffCheckbox = false;
            }
        },
        datatypeDiffCheckboxChange:function (flag) {
            this.checkboxChange(flag,"dataType",'diff');
            if(flag){
                this.datatypeMaxCheckbox = false;
                this.datatypeMinCheckbox = false;
                this.datatypeAveCheckbox = false;
            }
        },
        sourceTypeCoralCheckboxChange: function (flag) {
            this.checkboxChange(flag,"sourceType",'1');
            if(flag){
                this.sourceTypeXhaCheckbox = false;
            }
        },
        sourceTypeXhaCheckboxChange: function (flag) {
            this.checkboxChange(flag,"sourceType",'2');
            if(flag){
                this.sourceTypeCoralCheckbox = false;
            }
        }
    }
})
